package com.shudingke.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shudingke.domain.Cart;
import com.shudingke.domain.Products;
import com.shudingke.domain.vo.CartVo;
import com.shudingke.service.IProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2022-06-29
 */
@Controller
@RequestMapping("/products")
public class ProductsController {
    //获取redis数据库连接对象
    @Autowired
    RedisTemplate redisTemplate;
    
    @Autowired
    IProductsService service;
    
    //根据名称对产品进行模糊查询
    @RequestMapping("/findByName")
    public String findByName(String searchName, Model model,@RequestParam(defaultValue = "1") String page){
        model.addAttribute("searchName",searchName);
        //如果为空就查询所有
        Page<Products> p = null;
        Products products = new Products();
        if (StringUtils.isEmpty(searchName)){
            p = products.selectPage(new Page<Products>(Integer.parseInt(page), 12),null);
        }else {
            QueryWrapper<Products> qw = new QueryWrapper();
            qw.like("title",searchName);
            p = products.selectPage(new Page<Products>(Integer.parseInt(page), 12),qw);
        }
        //把查询出来的page对象存储到request域里面
        model.addAttribute("page",p);
        long pages = p.getTotal() % 12 == 0 ? p.getTotal() / 12 : p.getTotal() / 12 + 1;
        List pList = new ArrayList();
        //把页码存到集合里面去
        for (int i = 1; i <= pages; i++){
            pList.add(i);
        }
        List<Products> records = p.getRecords();
        model.addAttribute("pList", pList);
        model.addAttribute("max", pages);
        //model.addAttribute("records", records);
        
        //把每行可能要遍历的对象存到集合中
        
        List<Products> list1 = new ArrayList();
        List<Products> list2 = new ArrayList();
        List<Products> list3 = new ArrayList();
        for (int i = 0; i < records.size(); i++){
            if (i < 4){
                list1.add(records.get(i));
            }else if (4 <= i && i < 8){
                list2.add(records.get(i));
            }else if (8 <= i && i < 12){
                list3.add(records.get(i));
            }
        }
        model.addAttribute("list1", list1);
        model.addAttribute("list2", list2);
        model.addAttribute("list3", list3);
        return "search";
    }
    
    
    //通过a标签跳转具体产品页面
    @RequestMapping("/findOne")
    public String findOne(@RequestParam(defaultValue = "1") String id,Model model){
        Products pro = new Products();
        QueryWrapper<Products> qw = new QueryWrapper<>();
        qw.eq("id",Integer.parseInt(id));
        Products one = pro.selectOne(qw);
        model.addAttribute("nowProduct",one);
        model.addAttribute("searchName","");
        return "product";
    }
    
    
    
    //跳转到主页时一定要访问的方法
    //这个方法把新到好货和热销列表取出来存到request域
    
    @RequestMapping("/gotoMain")
    public String gotoMain(Model model){
        Products pro = new Products();
        
        //新到好货
        //先看看redis中有没有 没有就从数据库取出来
        QueryWrapper<Products> qw = new QueryWrapper<>();
        if (redisTemplate.opsForValue().get("newList") == null){
            qw.orderByDesc("modifyTime");
            Page<Products> p = pro.selectPage(new Page<Products>(1,4),qw);
            List<Products> newList = p.getRecords();
            redisTemplate.opsForValue().set("newList",newList);
        }
        
        List<Products> newList = (List<Products>)redisTemplate.opsForValue().get("newList");
        
        
        
        //热销列表  没有定义热销属性 就当做价格最高的几个
        //先看看redis中有没有 没有就从数据库取出来
        QueryWrapper<Products> qw1 = new QueryWrapper<>();
        
        if (redisTemplate.opsForValue().get("hotList") == null){
            qw.orderByDesc("price");
            Page<Products> p1 = pro.selectPage(new Page<Products>(1,4),qw1);
            List<Products> hotList = p1.getRecords();
            redisTemplate.opsForValue().set("hotList",hotList);
        }
        List<Products> hotList = (List<Products>)redisTemplate.opsForValue().get("hotList");
        model.addAttribute("newList",newList);
        model.addAttribute("hotList",hotList);
        return "main";
    }
    
    
    @RequestMapping("/buyAtOnce")
    @ResponseBody
    public String buyAtOnce(HttpServletRequest req,String pid,String num){
        String uuid = req.getSession().getAttribute("uid").toString();
        List<CartVo> list = new ArrayList<>();

        Cart cart = new Cart();
        cart.setUid(Integer.parseInt(uuid));
        cart.setPid(Integer.parseInt(pid));
        cart.setNum(Integer.parseInt(num));
        
        Products products = new Products();
        products.setId(Integer.parseInt(pid));
        Products byId = products.selectById();
        
        cart.setPrice(byId.getPrice());
        cart.insert();

        List<Cart> carts = cart.selectList(null);
        Cart cart1 = carts.get(carts.size() - 1);


        CartVo cartVo = new CartVo();
        cartVo.setCid(cart1.getCid());
        cartVo.setUid(cart1.getUid());
        cartVo.setPid(cart1.getPid());
        cartVo.setPrice(cart1.getPrice());
        cartVo.setNum(cart1.getNum());
        cartVo.setTitle(byId.getTitle());
        cartVo.setImage(byId.getImage());
        
        
        list.add(cartVo);
        req.getSession().setAttribute("payList",list);
        
        
        return "111";
    }
}

